"""
Case Type   : gs_checkse工具功能
Case Name   : 验证gs_checkse的A8检测项,确保WAL、开启归档模式信息记录级别配置检测与修复成功
Create At   : 2024/11/1
Owner       : lihongji
Description :
    1.设置wal_level=archive、archive_mode=off、hot_standby=off、
    max_wal_senders=16后检测A8项
    2.修复A8不符合安全项后再次检测,查看wal_level、archive_mode的值
    3.设置wal_level=logical、archive_mode=off、hot_standby=on、
    max_wal_senders=16后检测A8项
    4.修复A8不符合安全项后再次检测,查看wal_level、archive_mode的值
    5.设置wal_level=hot_standby、archive_mode=off、hot_standby=on、
    max_wal_senders=16后检测A8项
    6.修复A8符合安全项后再次检测,查看wal_level、archive_mode的值
    7.设置wal_level=logical、archive_mode=on、hot_standby=on、
    max_wal_senders=16后检测A8项
    8.修复A8符合安全项后再次检测,查看wal_level、archive_mode的值
    9.设置wal_level=archive、archive_mode=on、hot_standby=off、
    max_wal_senders=16后检测A8项
    10.修复A8不符合安全项后再次检测,查看wal_level、archive_mode的值
Expect      :
    1.设置成功，检测打印告警信息
    2.修复成功，不打印告警信息，wal_level、archive_mode的值分别为hot_standby、off
    3.设置成功，检测打印告警信息
    4.修复成功，不打印告警信息，wal_level、archive_mode的值分别为hot_standby、off
    5.设置成功，检测不打印告警信息
    6.修复成功，不打印告警信息，wal_level、archive_mode的值分别为hot_standby、off
    7.设置成功，检测打印告警信息
    8.修复成功，不打印告警信息，wal_level、archive_mode的值分别为hot_standby、on
    9.设置成功，检测打印告警信息
    10.修复成功，不打印告警信息，wal_level、archive_mode的值分别为hot_standby、on
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        step = ('----step1:设置wal_level=archive、archive_mode=off、'
                'hot_standby=off、max_wal_senders=16后检测A8项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        wal_level_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "wal_level=archive")
        archive_mode_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "archive_mode=off")
        hot_standby_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "hot_standby=off")
        max_wal_senders_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "max_wal_senders=16")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A8 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(wal_level_msg)
        self.assertTrue(archive_mode_msg)
        self.assertTrue(hot_standby_msg)
        self.assertTrue(max_wal_senders_msg)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Warning", check_res, '执行失败' + step)
        self.assertTrue(restart_msg)

        step = ('----step2:修复A8不符合安全项再次检测,查看wal_level、archive_mode的值'
                'expect:修复成功，不打印告警信息，wal_level、archive_mode的值分别为'
                'hot_standby、off----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B8 --detail;'
                          f'gs_checkse -i A8 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show wal_level;show archive_mode;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('hot_standby', create_res, '执行失败' + step)
        self.assertIn('off', create_res, '执行失败' + step)
        self.assertIn("[ Set Backup configuration ]                    "
                      "        : Normal", check_res, '执行失败' + step)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Normal", check_res, '执行失败' + step)

        step = ('----step3:设置wal_level=logical、archive_mode=off、'
                'hot_standby=on、max_wal_senders=16后检测A8项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        wal_level_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "wal_level=logical")
        archive_mode_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "archive_mode=off")
        hot_standby_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "hot_standby=on")
        max_wal_senders_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "max_wal_senders=16")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A8 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(wal_level_msg)
        self.assertTrue(archive_mode_msg)
        self.assertTrue(hot_standby_msg)
        self.assertTrue(max_wal_senders_msg)
        self.assertTrue(restart_msg)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Warning", check_res, '执行失败' + step)

        step = ('----step4:修复A8不符合安全项再次检测,查看wal_level、archive_mode的值'
                'expect:修复成功，不打印告警信息，wal_level、archive_mode的值分别为'
                'hot_standby、off----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B8 --detail;'
                          f'gs_checkse -i A8 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show wal_level;show archive_mode;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('hot_standby', create_res, '执行失败' + step)
        self.assertIn('off', create_res, '执行失败' + step)
        self.assertIn("[ Set Backup configuration ]                    "
                      "        : Normal", check_res, '执行失败' + step)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Normal", check_res, '执行失败' + step)

        step = ('----step5:设置wal_level=hot_standby、archive_mode=off、'
                'hot_standby=on、max_wal_senders=16后检测A8项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        wal_level_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "wal_level=hot_standby")
        archive_mode_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "archive_mode=off")
        hot_standby_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "hot_standby=on")
        max_wal_senders_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "max_wal_senders=16")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A8 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(wal_level_msg)
        self.assertTrue(archive_mode_msg)
        self.assertTrue(hot_standby_msg)
        self.assertTrue(max_wal_senders_msg)
        self.assertTrue(restart_msg)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Normal", check_res, '执行失败' + step)

        step = ('----step6:修复A8不符合安全项再次检测,查看wal_level、archive_mode的值'
                'expect:修复成功，不打印告警信息，wal_level、archive_mode的值分别为'
                'hot_standby、off----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B8 --detail;'
                          f'gs_checkse -i A8 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show wal_level;show archive_mode;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('hot_standby', create_res, '执行失败' + step)
        self.assertIn('off', create_res, '执行失败' + step)
        self.assertIn("[ Set Backup configuration ]                    "
                      "        : Normal", check_res, '执行失败' + step)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Normal", check_res, '执行失败' + step)

        step = ('----step7:设置wal_level=logical、archive_mode=on、'
                'hot_standby=on、max_wal_senders=16后检测A8项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        wal_level_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "wal_level=logical")
        archive_mode_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "archive_mode=on")
        hot_standby_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "hot_standby=on")
        max_wal_senders_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "max_wal_senders=16")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A8 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(wal_level_msg)
        self.assertTrue(archive_mode_msg)
        self.assertTrue(hot_standby_msg)
        self.assertTrue(max_wal_senders_msg)
        self.assertTrue(restart_msg)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Warning", check_res, '执行失败' + step)

        step = ('----step8:修复A8不符合安全项再次检测,查看wal_level、archive_mode的值'
                'expect:修复成功，不打印告警信息，wal_level、archive_mode的值分别为'
                'hot_standby、on----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B8 --detail;'
                          f'gs_checkse -i A8 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show wal_level;show archive_mode;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('hot_standby', create_res, '执行失败' + step)
        self.assertIn('on', create_res, '执行失败' + step)
        self.assertIn("[ Set Backup configuration ]                    "
                      "        : Normal", check_res, '执行失败' + step)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Normal", check_res, '执行失败' + step)

        step = ('----step9:设置wal_level=archive、archive_mode=on、'
                'hot_standby=off、max_wal_senders=16后检测A8项'
                'expect:设置成功，检测打印告警信息----')
        self.log.info(step)
        wal_level_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "wal_level=archive")
        archive_mode_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "archive_mode=on")
        hot_standby_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "hot_standby=off")
        max_wal_senders_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "max_wal_senders=16")
        restart_msg = self.sh_primary.restart_db_cluster()
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i A8 --detail')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        self.assertTrue(wal_level_msg)
        self.assertTrue(archive_mode_msg)
        self.assertTrue(hot_standby_msg)
        self.assertTrue(max_wal_senders_msg)
        self.assertTrue(restart_msg)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Warning", check_res, '执行失败' + step)

        step = ('----step10:修复A8不符合安全项后检测,查看wal_level、archive_mode的值'
                'expect:修复成功，不打印告警信息，wal_level、archive_mode的值分别为'
                'hot_standby、on----')
        self.log.info(step)
        gs_checkse_cmd = (f'source {self.env_path};'
                          f'gs_checkse -i B8 --detail;'
                          f'gs_checkse -i A8 --detail;')
        self.log.info(gs_checkse_cmd)
        check_res = self.common.get_sh_result(self.primaryNode, gs_checkse_cmd)
        check_sql = "show wal_level;show archive_mode;"
        create_res = self.sh_primary.execut_db_sql(check_sql)
        self.log.info(create_res)
        self.assertIn('hot_standby', create_res, '执行失败' + step)
        self.assertIn('on', create_res, '执行失败' + step)
        self.assertIn("[ Set Backup configuration ]                    "
                      "        : Normal", check_res, '执行失败' + step)
        self.assertIn("[ Backup configuration ]                        "
                      "        : Normal", check_res, '执行失败' + step)

    def tearDown(self):
        step = '----step11:清理环境 expect:成功----'
        self.log.info(step)
        wal_level_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "wal_level=hot_standby")
        archive_mode_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "archive_mode=off")
        hot_standby_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "hot_standby=on")
        max_wal_senders_msg = self.sh_primary.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG, "max_wal_senders=16")
        restart_msg = self.sh_primary.restart_db_cluster()
        self.assertTrue(wal_level_msg)
        self.assertTrue(archive_mode_msg)
        self.assertTrue(hot_standby_msg)
        self.assertTrue(max_wal_senders_msg)
        self.assertTrue(restart_msg)
        self.log.info(f'----{os.path.basename(__file__)} end----')